package com.task.controller.annotation.impl;

import com.task.controller.annotation.AsyncTask;
import com.task.controller.annotation.CurrentTask;
import com.task.controller.annotation.Parser;
import com.task.controller.annotation.UITask;
import com.task.controller.entity.Mission;
import com.task.controller.factory.ParserFactory;
import com.task.controller.manager.impl.AsyncMethodManager;
import com.task.controller.manager.impl.CurrentMethodManager;
import com.task.controller.manager.impl.UIMethodManager;
import com.task.controller.param.impl.ToObjectParser;

import java.lang.reflect.Method;

public class TaskImpl {

    public static void initTask(Object o) {
        if (o == null) {
            return;
        }
        Class<?> aClass = o.getClass();
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            boolean isSuccess = initAsync(o, method);
            if (!isSuccess) {
                isSuccess = initCurrent(o, method);
            }
            if (!isSuccess){
                initUI(o, method);
            }
        }
    }

    private static boolean initAsync(Object o, Method method) {
        AsyncTask asyncTask = method.getDeclaredAnnotation(AsyncTask.class);
        if (asyncTask == null) {
            return false;
        }
        Parser parser = method.getDeclaredAnnotation(Parser.class);
        Mission mission = new Mission();
        mission.setMethod(method);
        mission.setObject(o);
        if (parser == null) {
            mission.setParse(ParserFactory.getParser(ToObjectParser.class));
        } else {
            mission.setParse(ParserFactory.getParser(parser.cls()));
        }
        AsyncMethodManager.getInstance().register(asyncTask.value(), mission);
        return true;
    }

    private static boolean initCurrent(Object o, Method method) {
        CurrentTask currentTask = method.getDeclaredAnnotation(CurrentTask.class);
        if (currentTask == null) {
            return false;
        }
        Parser parser = method.getDeclaredAnnotation(Parser.class);
        Mission mission = new Mission();
        mission.setMethod(method);
        mission.setObject(o);
        if (parser == null) {
            mission.setParse(ParserFactory.getParser(ToObjectParser.class));
        } else {
            mission.setParse(ParserFactory.getParser(parser.cls()));
        }
        CurrentMethodManager.getInstance().register(currentTask.value(), mission);
        return true;
    }

    private static void initUI(Object o, Method method) {
        UITask uiTask = method.getDeclaredAnnotation(UITask.class);
        if (uiTask == null) {
            return;
        }
        Parser parser = method.getDeclaredAnnotation(Parser.class);
        Mission mission = new Mission();
        mission.setMethod(method);
        mission.setObject(o);
        if (parser == null) {
            mission.setParse(ParserFactory.getParser(ToObjectParser.class));
        } else {
            mission.setParse(ParserFactory.getParser(parser.cls()));
        }
        UIMethodManager.getInstance().register(uiTask.value(), mission);
    }
}
